package com.kgc.smbms.controller;

import com.kgc.smbms.pojo.SmbmsUser;
import com.kgc.smbms.service.SmbmsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class SmbmsUserController {

    //引入业务层对象
    @Autowired
    private SmbmsUserService userService;

    //编写退出功能
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        //删除存储到session域中的对象
        session.removeAttribute("smbmsUser");
        //退出后自动中转到登录页面
        return "redirect:/login.jsp";
    }






    /**
     * 这个参数页面可以不传递，后台会创建这个参数,然后给一个空字符串作为默认值
     * @RequestParam(required = false,defaultValue = "")
     * @param usercode
     * @param userpassword
     * @param model
     * @return
     */
    @RequestMapping("/login")
    public String login(@RequestParam(required = false,defaultValue = "") String usercode,
                        @RequestParam(required = false,defaultValue = "") String userpassword,
                        Model model, HttpSession session){

        SmbmsUser smbmsUser = userService.loginUser(usercode, userpassword);

        if(null == smbmsUser){
            model.addAttribute("error","用户名或者密码错误");
            return "forward:/login.jsp";
        }

        session.setAttribute("smbmsUser",smbmsUser);
        //如果登录成功，去到首页
        return "frame";
    }
}
